package com.yangoxix.lovegoing.controller;

import com.yangoxix.lovegoing.service.SignService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * The type Sign controller.
 *
 * @program: lovegoing
 * @description: 签到功能的接口
 * @author: chenzou
 * @create: 2021 -09-23 09:13
 */
@Controller
@RequestMapping("sign")
@Api(tags="签到操作接口")
public class SignController {
    @Autowired
    private SignService service;

    /**
     * Start sign response entity开始签到的接口
     *
     * @param title the title
     * @param hours the hours
     * @return the response entity
     */
    @GetMapping("startSign")
    @ResponseBody
    public ResponseEntity startSign(String title, @RequestParam(required = false, defaultValue = "3") int hours) {
        try {
            String sign = service.isSign(title);
            if (sign != null && !"".equals(sign)) {
                return new ResponseEntity(sign, HttpStatus.INTERNAL_SERVER_ERROR);

            } else {
                String s = service.beginSign(hours, title);
                return new ResponseEntity(s, HttpStatus.OK);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("fail", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * To sign response entity.进行签到
     *
     * @param username     the username
     * @param activityname the activityname
     * @return the response entity
     */
    @GetMapping("toSign")
    @ResponseBody
    public ResponseEntity toSign(String username, String activityname, String code) {
        try {
            if (service.isApplyed(activityname, username)) {
                String isSigned = service.checkIsSigned(username, activityname);
                if ("success".equals(isSigned)) {
                    //没有签到过
                    if (service.isOverTime(activityname)) {
                        String checkcode = service.checkcode(activityname, code);
                        if ("correct".equals(checkcode)) {
                            service.toSign(username, activityname);
                            return new ResponseEntity("success", HttpStatus.OK);
                        } else {
                            return new ResponseEntity(checkcode, HttpStatus.INTERNAL_SERVER_ERROR);
                        }
                    } else {
                        return new ResponseEntity("超时", HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                }else{
                    return new ResponseEntity(isSigned, HttpStatus.INTERNAL_SERVER_ERROR);
                }

            } else {
                return new ResponseEntity("没有报名活动", HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("未知错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * To sign response entity.判断活动是否开始签到
     *
     * @param activityname the activityname
     * @return the response entity
     */
    @GetMapping("isBeginSign")
    @ResponseBody
    public ResponseEntity toSign(String activityname) {
        String sign = service.isSign(activityname);
        if (sign == null) {
            return new ResponseEntity("fail", HttpStatus.INTERNAL_SERVER_ERROR);
        } else {
            return new ResponseEntity(sign, HttpStatus.OK);
        }
    }

    @GetMapping("showSignedUser")
    @ResponseBody
    public ResponseEntity showSignedUser(String title) {
        try {
            List<String> signUser = service.getSignUser(title);
            return new ResponseEntity(signUser, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("fail", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
